<?php
namespace Admin\Controller;
use Admin\Model\AdminModel;
use Think\Controller;
use Think\Upload;

class IndexController extends Controller {
    public function index(){

      if(session('?uname')){
          $this->display();
      }
        else{
            $this->error("请登录",login,3);
        }
}
    //生成验证码的方法
    public function captcha(){
        //配置
        $cfg = array(
            'fontSize'  =>  12,              // 验证码字体大小(px)
            'useCurve'  =>  false,            // 是否画混淆曲线
            'useNoise'  =>  false,            // 是否添加杂点
            'length'    =>  4,               // 验证码位数
            'imageH'	=>	38,
            'imageW'	=>	91,
            'bg'        =>  array(93,202,27),
            'fontttf'   =>  '4.ttf',// 验证码字体，不设置随机获取
        );
        //实例化验证码类
        $verify = new \Think\Verify($cfg);
        //输出验证码
        $verify -> entry();
    }
    //用户登录
public function login(){

    $this->display();
}
    //管理员登录
    public function checklogin(){
        //检验账号名和密码的一般先检验验证码是否正确验证码正确
        //验证正确之后把用户名和id给session
        $post=I('post.');
        $verify=new\Think\Verify();
        $result=$verify->check($post['captcha']);
          if($result){
         $pd=M('admin');
              unset($post['captcha']);
              $data=$pd->where($post)->find();
              if($data){
                  session('uid',$data['uid']);
                  session('uname',$data['uname']);
                  $this->success("登录成功",index,3);
              }
              else{
                  $this->error("用户或者密码错误。。",login,3);;
              }
          }
        else{
            $this->error("验证码错误",login,3);
        }

    }
     //退出登录
    public function logout(){
        //清除session
        session(null);
        //跳转到登录页面
        $this -> success('退出成功',login,3);
    }

   //展示机械类型添加页面
      public  function addtype(){
        $this->display();
      }

    //进行机械类型的查看
    public function  showtypeinfo(){
      $model=M('typeinfo');
        $count=$model->count();
        $Page=new\Think\Page($count,1);
        $show=$Page->show();
        $list = $model->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出$this->display();
        $this->assign('list',$list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出$this->display(); // 输出模板
        $this->display("showtypeinfo");
    }
    //进行数据的添加操作
    public  function  addtypeinfo(){
    $post=I('post.');
    $model=M('typeinfo');
    $model->data($post)->add();
    $pf=new IndexController();
    $pf->showtypeinfo();

}
    // 进行机械类型的删除
  public function  deletetype(){
      $tid=$_GET["tid"];
      $model=M('typeinfo');
      $model->where("tid=".$tid)->delete();
      $pf=new IndexController();
      $pf->showtypeinfo();


  }
    //点修改获取该类型的值
    public function  getonetype(){
        $tid=$_GET["tid"];
        $model=M('typeinfo');
        $data=$model->where("tid=".$tid)->find();
        $this->assign("onelx",$data);
        $this->display();
    }
    //修改机械类型
    public function  updatatype(){

       $post=I('post.');
     $model=M('typeinfo');
        $data=array(
                'tid' =>$post['tid'],
                'tname'=>$post['tname'],
              'tcontent'=>$post['tcontent']
        );
        $model->save($data);
        $pf=new IndexController();
        $pf->showtypeinfo();

    }
    //查看产品
    public function showproduct(){
       $model=M('typeproduct');
       $data= $model->select();
       $this->assign("cl",$data);
        $this->display("showproduct");
    }
    //添加产品页面
    public function addproduct(){
        $pf=M('typeinfo');
        $data=$pf->select();
        $this->assign("ll",$data);
        $this->display();
    }
    //删除产品
    public function deleteproduct(){
        $pid=$_GET["pid"];
        $pd=M('product');
      $pd->where("pid=".$pid)->delete();
        $cf=new IndexController();
        $cf->showproduct();

    }
    //点击修改得到产品的信息进行修改
    public function getoneproduct(){
        $pid=$_GET["pid"];
        $model=M("typeproduct");
        $mf=M("typeinfo");
        $typedata=$mf->select();
        $this->assign("td",$typedata);
        $data=$model->where("pid=".$pid)->find();
        $this->assign("hh",$data);
        $this->display("getoneproduct");
    }
    //修改产品
    public function updateproduct(){
        $tp=M("product");
        $config = array(
            'maxSize' => 3145728,                             // 设置附件上传大小，默认3M
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),  // 设置附件上传类型
            'rootPath' => './',                                // 设置根路径
            'savePath' => 'Upload/image/'                      // 设置附件上传目录
        );
        $upload = new\Think\Upload($config);
        $info = $upload->upload();
      if($info){
          foreach ($info as $file) {
              $_POST["pic"]=$file['savepath'] . $file['savename'];
         }
     }
        $tp->create();
        $tp->save();
        $cf=new IndexController();
        $cf->showproduct();
    }
    //开始添加产品
    public function  addsproduct()
    {
        $config = array(
            'maxSize' => 3145728,                             // 设置附件上传大小，默认3M
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),  // 设置附件上传类型
            'rootPath' => './',                                // 设置根路径
            'savePath' => 'Upload/image/'                      // 设置附件上传目录
        );
        $upload = new\Think\Upload($config);
        // 上传文件
        $info = $upload->upload();
        foreach ($info as $file) {
            $_POST['pic']= $file['savepath'] . $file['savename'];
        }
             $pd=M('product');
             $pd->create();
             $pd->add();
           $nf=new IndexController();
           $nf->showproduct();
    }

//添加新闻页面
public function  addnew(){
//    $time=time();
//    $time->assign('time',$time);

    $this->display();
}
    //开始添加新闻
 public function addanew(){
    $post=I('post.');
//     var_dump($post);
     $newsdate=time();
     $this->assign('newsdate',$newsdate);
    $model=M('newsinfo');

    $model->data($post)->add();
 }
    //查看新闻
    public function shownews(){
         $mm=M('newsinfo');
         $data=$mm->select();
         $this->assign("kl",$data);
        $this->display("shownews");
    }
    //删除新闻
    public  function  deletnews(){
         $nid= $_GET['nid'];
        $mf=M('newsinfo');
        $mf->where("nid=".$nid)->delete();
       $md=new IndexController();
        $md->shownews();

    }
    //得到要修改的新闻的
    public  function  getonenew(){
        $nid=$_GET["nid"];
        $model=M('newsinfo');

        $data=$model->where("nid=".$nid)->find();

//        $newsdate=time();
//        $this->assign('newsdate',$newsdate);

        $this->assign("da",$data);
       $this->display("getonenew");
    }

    //修改新闻
    public function updatenewinfo(){
        $newsdate=time();
        $this->assign('newsdate',$newsdate);
        $md=M('newsinfo');


        $md->create();
        $md->save();
    }

//添加后台管理人员页面
public  function  adduser(){
    $this->display("adduser");
}
    //添加管理表
    public function addusera(){
             $post=I('post.');
             $model=D('admin');
        if($post['uname']==null){
            $this->error("账户名不能为空",adduser,3);
        }
        else{
            $data=$model->create();
            if(!$data){
                $this->error($model->getError());exit;
            }
            else{
                if($post['upwd']==null){
                    $this->error("密码不能为空",adduser,3);
                }
                else{
                    if($post['repwd']==null){
                        $this->error("确认密码不能为空",adduser,3);
                    }
                    else{
                        if($post['upwd']==$post['repwd']){
                            $model->data($post)->add();
                            $this->success("添加成功",adduser,3);
                        }
                        else{
                            $this->error("两次密码不相同",adduser,3);
                        }
                    }
                }

            }
        }
         }
    //查看管理员信息
    public function showuseinfo(){
        $model=M('admin');
        $data=$model->select();
        $this->assign("da",$data);
        $this->display("showuserinfo");
    }
    //删除用户
    public function deleteuser(){
              $uid=$_GET["uid"];
               $mf=M('admin');
        $mf->where("uid=".$uid)->delete();

        $lf=new IndexController();
        $lf->showuseinfo();
    }
    //得到用户数据
    public  function  getoneuser(){
        $uid=$_GET["uid"];
        $mf=M('admin');
        $dat=$mf->where("uid=".$uid)->find();
        $this->assign("mm",$dat);
        $this->display();
    }

    //修改管理员信息
    public function  updateuserinfo(){
          $post=I('post.');
          $pd=M('admin');
        if($post['upwd']==$post['repwd']){
            $data=array(
                'uid' =>$post['uid'],
                'uname'=>$post['uname'],
                'upwd'=>$post['upwd'],
            );
           $da= $pd->save($data);
            if($da){
                $this->success("密码修改成功",showuseinfo,3);
            }
            else{
                $this->error("密码修改失败",showuseinfo,3);
            }
        }
        else{
            $this->error("两次密码不一致",showuseinfo,3);
        }
    }

    //添加产品维修案例页面
    public function  addcpweixiu(){
        $this->display();
    }
    //查看产品案列维修
    public function  showcpwx(){
        $hh=M('pweixiu');
        $cp=$hh->select();
        $this->assign("kl",$cp);
        $this->display("showcpwx");
    }
    //开始添加维修
    public  function  addcpneir(){
        $post=I('post.');
        $md=M('pweixiu');
       $md->data($post)->add();
        $mn=new IndexController();
        $mn->showcpwx();
}


    //得到它的值维修
    public function  getcpwx(){
        $cpid=$_GET['cpid'];
        $md=M('pweixiu');
       $dd= $md->where('cpid='.$cpid)->find();
        $this->assign("cp",$dd);
     $this->display();
    }
    //修改维修产品
    public function updatawx(){

       $md=M('pweixiu');
       $md->create();
      $md->save();
        $mn=new IndexController();
        $mn->showcpwx();

    }
    //删除维修产品
    public function deltecpwx(){
        $cpid=$_GET['cpid'];
        $md=M('pweixiu');
        $md->where('cpid='.$cpid)->delete();
        $mn=new IndexController();
        $mn->showcpwx();
    }
    public function showaz(){
        $hh=M('azcase');
        $cp=$hh->select();
        $this->assign("kl",$cp);
        $this->display("showaz");

    }

//展示安装页面
    public function addaz(){
        $this->display("addaz");
    }
    //开始添加安装案例
    public  function addazan(){
        $post=I('post.');
        $md=M('azcase');
        $md->data($post)->add();
        $ml=new IndexController();
        $ml->showaz();
    }
    public function deleteaz(){

        $aid=$_GET['aid'];
        $md=M('azcase');
        $md->where('aid='.$aid)->delete();
        $ml=new IndexController();
        $ml->showaz();
    }
    public function getaz(){

        $aid=$_GET['aid'];
        $md=M('azcase');
        $dd= $md->where('aid='.$aid)->find();
        $this->assign("cp",$dd);
        $this->display();
    }
    public function updateaz(){


        $md=M('azcase');
        $md->create();
        $md->save();
        $ml=new IndexController();
        $ml->showaz();

    }


public  function  showmessage(){

    $this->display("showmessage");

}





}